package com.jianan.mis.app;

import org.eclipse.jface.action.Action;
import org.eclipse.jface.action.ICoolBarManager;
import org.eclipse.jface.action.IMenuManager;
import org.eclipse.jface.action.IStatusLineManager;
import org.eclipse.jface.action.IToolBarManager;
import org.eclipse.jface.action.MenuManager;
import org.eclipse.jface.action.Separator;
import org.eclipse.jface.action.ToolBarManager;
import org.eclipse.swt.SWT;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.actions.ActionFactory;
import org.eclipse.ui.actions.ActionFactory.IWorkbenchAction;
import org.eclipse.ui.application.ActionBarAdvisor;
import org.eclipse.ui.application.IActionBarConfigurer;

import com.jianan.mis.app.menu.ShowMenuAction;
import com.jianan.mis.data.domain.Session;
import com.jianan.mis.sys.IImageKeys;
import com.jianan.mis.sys.ImageManager;
import com.jianan.mis.sys.StatusBarContribution;
import com.jianan.mis.sys.UpdateAction;

/**
 * An action bar advisor is responsible for creating, adding, and disposing of
 * the actions added to a workbench window. Each window will be populated with
 * new actions.
 */
public class ApplicationActionBarAdvisor extends ActionBarAdvisor {

	private ShowMenuAction showMenuAction;

	private IWorkbenchAction preferencesAction;
	
	private RestartAction restartAction;
	
	private UpdateAction updateAction;

	private IWorkbenchAction helpAction;

	private IWorkbenchAction aboutAction;

	private IWorkbenchAction exitAction;

	public ApplicationActionBarAdvisor(IActionBarConfigurer configurer) {
		super(configurer);
	}

	protected void makeActions(final IWorkbenchWindow window) {

		showMenuAction = new ShowMenuAction(window);
		showMenuAction.setAccelerator(SWT.CTRL | 'M');
		register(showMenuAction);

		preferencesAction = ActionFactory.PREFERENCES.create(window);
		preferencesAction.setImageDescriptor(ImageManager
				.getImageDescriptor(IImageKeys.PREFERENCE));
		register(preferencesAction);
		
		restartAction = new RestartAction();
		restartAction.setAccelerator(SWT.CTRL | 'R');
		register(restartAction);
		
		updateAction = new UpdateAction();
		updateAction.setAccelerator(SWT.CTRL | 'U');
		register(updateAction);

		helpAction = ActionFactory.HELP_CONTENTS.create(window);
		helpAction.setAccelerator(SWT.F1);
		register(helpAction);

		aboutAction = ActionFactory.ABOUT.create(window);
		aboutAction.setAccelerator(SWT.CTRL | SWT.SHIFT | SWT.F1);
		register(aboutAction);

		exitAction = ActionFactory.QUIT.create(window);
		exitAction.setAccelerator(SWT.CTRL | 'Q');
		exitAction.setImageDescriptor(ImageManager.getImageDescriptor(IImageKeys.EXIT));
		register(exitAction);
	}

	protected void fillMenuBar(IMenuManager menuBar) {

		MenuManager mainMenu = new MenuManager("功能(&M)", "Main");
		mainMenu.add(showMenuAction);
		mainMenu.add(preferencesAction);
		mainMenu.add(new Separator());
		mainMenu.add(restartAction);
		mainMenu.add(exitAction);
		MenuManager helpMenu = new MenuManager("帮助(&H)", "Help");
		helpMenu.add(updateAction);
		helpMenu.add(helpAction);
		helpMenu.add(aboutAction);

		menuBar.add(mainMenu);
		menuBar.add(helpMenu);
	}

	protected void fillCoolBar(ICoolBarManager coolBar) {
		IToolBarManager toolbar = new ToolBarManager(coolBar.getStyle());
		coolBar.add(toolbar);

		toolbar.add(showMenuAction);
		toolbar.add(new Separator());
		toolbar.add(preferencesAction);
		toolbar.add(new Separator());
		toolbar.add(restartAction);
		toolbar.add(new Separator());
		toolbar.add(updateAction);
		toolbar.add(new Separator());
		toolbar.add(exitAction);
	}

	protected void fillStatusLine(IStatusLineManager statusLine) {
		StringBuffer msg = new StringBuffer();
		msg.append("登陆用户:   ");
		msg.append(Session.getInstance().getEmployeeName());
		StatusBarContribution statusBarContribution = new StatusBarContribution(msg.toString());
		statusBarContribution.setVisible(true);
		statusLine.add(statusBarContribution);
	}
	
	private class RestartAction extends Action {
		public final static String ID = "com.jianan.mis.app.restart";

		public RestartAction() {
			setId(ID);
			setActionDefinitionId(ID);
			setText("重启系统(&R)");
			setToolTipText("重新启动系统");
			setImageDescriptor(ImageManager
					.getImageDescriptor(IImageKeys.RESTART));
		}

		public void run() {
			PlatformUI.getWorkbench().restart();
		}
	}
}
